package com.gugee.ins.data.blogger.statistics.core;


import com.gugee.ins.common.dal.service.blogger.InsBloggerMainDao;
import com.gugee.ins.common.dal.service.dic.InsDicFollowerSizeDao;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerCfsAvgDao;
import com.gugee.ins.data.blogger.statistics.config.BloggerStatisticsAppConfig;
import com.gugee.ins.data.common.msg.InsMBIdenticalLevelMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 统计同分类同量级粉丝博主平均值
 */
@Service
@Slf4j
public class StIdenticalLevelBloggerService {

    @Autowired
    private InsStBloggerCfsAvgDao insStBloggerCfsAvgDao;

    @Autowired
    private InsBloggerMainDao insBloggerMainDao;

    @Autowired
    private InsDicFollowerSizeDao insDicFollowerSizeDao;

    @Autowired
    private BloggerStatisticsAppConfig bloggerStatisticsAppConfig;


    public void handler(List<InsMBIdenticalLevelMessage> insMBIdenticalLevelMessages){
        try{

            for (InsMBIdenticalLevelMessage insMBIdenticalLevelMessage : insMBIdenticalLevelMessages) {
                statistics(insMBIdenticalLevelMessage.getId());
            }

        }catch (Exception e){
            log.error("StIdenticalLevelBloggerService error!",e);
        }
    }

    private void statistics(long fsId){
        // 时间区间
        long timeRang = System.currentTimeMillis() / 1000 - bloggerStatisticsAppConfig.getLatestDays() * 24 * 3600;
        val insStBloggerCategoryFs = insStBloggerCfsAvgDao.getById(fsId);
        val insDicFollowerSize = insDicFollowerSizeDao.getById(insStBloggerCategoryFs.getFollowerSize());
        //同量级博主个数
        long mbCount = insBloggerMainDao.countByCategoryAndFollower(insStBloggerCategoryFs.getCategory(),insDicFollowerSize.getSizeStart(),insDicFollowerSize.getSizeEnd());

        if(mbCount > 0){
            //全部帖子参与率
            double mediaEr = insBloggerMainDao.stBMSumMediaErByCategoryAndFollower(insStBloggerCategoryFs.getCategory(),insDicFollowerSize.getSizeStart(),insDicFollowerSize.getSizeEnd(),timeRang,null);
            insStBloggerCategoryFs.setMediaEr(mediaEr/mbCount);


            //广告帖子参与率
            double adMediaEr = insBloggerMainDao.stBMSumMediaErByCategoryAndFollower(insStBloggerCategoryFs.getCategory(),insDicFollowerSize.getSizeStart(),insDicFollowerSize.getSizeEnd(),timeRang,true);
            insStBloggerCategoryFs.setAdMediaEr(adMediaEr/mbCount);
        }


        //真实粉丝占比
        double realFollowerRate = insBloggerMainDao.stBMAvgRealFollowerRateByCategoryAndFollower(insStBloggerCategoryFs.getCategory(),insDicFollowerSize.getSizeStart(),insDicFollowerSize.getSizeEnd());
        insStBloggerCategoryFs.setRealFollowerRate(realFollowerRate);
        //粉丝增长率
        double followerGrowRate = insBloggerMainDao.stBMAvgFollowerGrowRateByCategoryAndFollower(insStBloggerCategoryFs.getCategory(),insDicFollowerSize.getSizeStart(),insDicFollowerSize.getSizeEnd());
        insStBloggerCategoryFs.setFollowerGrowthRate(followerGrowRate);
        insStBloggerCfsAvgDao.updateById(insStBloggerCategoryFs);
    }
}
